package cn.wuyu.baselibrary.net.http;


import java.util.concurrent.TimeUnit;

import cn.wuyu.baselibrary.app.APPConfig;
import cn.wuyu.baselibrary.net.OkHttpInterceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Retrofit管理
 */
public class RetrofitManager {
    private static RetrofitManager sApiManager;

    //    private static OkHttpClient mClient;
//    private Retrofit retrofit;
//    private AirFareApiService mGithubApi;
//    public T data;
    private RetrofitManager() {
    }

    public static RetrofitManager getInstence() {
        if (sApiManager == null) {
            synchronized (RetrofitManager.class) {
                if (sApiManager == null) {
                    sApiManager = new RetrofitManager();
                }
            }
        }
        String errorCode = HttpException.errorCode;
        return sApiManager;
    }

//    /**
//     * 创建OkHttpClient
//     */
//    private OkHttpClient buildOkHttpClient() {
//        OkHttpClient.Builder builder = new OkHttpClient.Builder()
//                .addInterceptor(new OkHttpInterceptor());
//        if (APPConfig.DEBUG) {
//            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//            builder.addInterceptor(loggingInterceptor);
//        }
//        builder.connectTimeout(100, TimeUnit.SECONDS);
//        builder.readTimeout(100, TimeUnit.SECONDS);
//        builder.writeTimeout(100, TimeUnit.SECONDS);
//        return builder.build();
//    }

    private OkHttpClient mClient;
    private OkHttpClient buildOkHttpClient(){
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        if (APPConfig.DEBUG){
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        mClient = new OkHttpClient.Builder()
                .addInterceptor(new OkHttpInterceptor())
                .addInterceptor(loggingInterceptor)
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
        return mClient;
    }

    /**
     * 为不同域名创建不同 Retrofit实例 实现动态切换域名
     */
    private Retrofit hotelRetrofit = null;

    public <T> T createWuYuService(Class<T> serviceClass) {
        if (hotelRetrofit==null){
            hotelRetrofit = new Retrofit.Builder()
                    .baseUrl(APPConfig.Network.WU_YU_BASE_URL)
                    .client(buildOkHttpClient())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return hotelRetrofit.create(serviceClass);
    }

}
